package zhihui.mobile.ui.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zgm.utils.DensityUtil;
import com.zgm.utils.StringUtils;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.config.Config;

/**
 * Created by 张公铭 on 2017/8/23 0023.
 */

public class ShopItemUtils
{

    public interface IItemOnClickListener
    {
        void goShopDetails(int shopId, int type);
    }

    public static void createdItem(Context context, List<SpuInfo> list, ViewGroup root, final IItemOnClickListener listener)
    {
        ImageView iv_left = null;
        TextView tv_name = null;
        TextView tv_price = null;
        TextView tv_xl = null;
        LinearLayout ly_left = null;
        LinearLayout ly_right = null;
        ImageView iv_right = null;
        TextView tv_right_name = null;
        TextView tv_right_price = null;
        TextView tv_right_xl = null;
        for (int i = 0; i < list.size(); i += 2)
        {
            View view = LayoutInflater.from(context).inflate(R.layout.shop_item_utils, null);
            if (i != 0)
            {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.setMargins(0, DensityUtil.dip2px(context, 10), 0, 0);
                view.setLayoutParams(lp);
            }
            iv_left = (ImageView) view.findViewById(R.id.iv_recommend_left_img);
            tv_name = (TextView) view.findViewById(R.id.tv_recommend_left_name);
            tv_price = (TextView) view.findViewById(R.id.tv_recommend_left_price);
            tv_xl = (TextView) view.findViewById(R.id.tv_recommend_left_xl);
            ly_left = (LinearLayout) view.findViewById(R.id.ly_recommend_left);

            ly_right = (LinearLayout) view.findViewById(R.id.ly_recommend_right);
            iv_right = (ImageView) view.findViewById(R.id.iv_recommend_right_img);
            tv_right_name = (TextView) view.findViewById(R.id.tv_recommend_right_name);
            tv_right_price = (TextView) view.findViewById(R.id.tv_recommend_right_price);
            tv_right_xl = (TextView) view.findViewById(R.id.tv_recommend_right_xl);

            final SpuInfo lefSpu = list.get(i);
            ImageLoader.getInstance().displayImage(Config.FILE_URL + lefSpu.getSpuimg(), iv_left);
            tv_name.setText(lefSpu.getTitle());
            tv_price.setText("￥" + StringUtils.doubleFormat((double) lefSpu.getMinprice() / 100));
            tv_xl.setText("已售:" + lefSpu.getSales());
            ly_left.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    listener.goShopDetails(lefSpu.getId(), 0);
                }
            });

            if ((i + 1) < list.size())
            {
                final SpuInfo rightSpu = list.get(i + 1);
                ImageLoader.getInstance().displayImage(Config.FILE_URL + rightSpu.getSpuimg(), iv_right);
                tv_right_name.setText(rightSpu.getTitle());
                tv_right_price.setText("￥" + StringUtils.doubleFormat((double) rightSpu.getMinprice() / 100));
                tv_right_xl.setText("已售:" + rightSpu.getSales());
                ly_right.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        listener.goShopDetails(rightSpu.getId(), 0);
                    }
                });
            } else
            {
                ly_right.setVisibility(View.GONE);
            }
            root.addView(view);
        }
    }
}
